package com.sk.bilibili.view.diy.bottomDialog;


import android.content.Context;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.TextView;
import android.widget.Toast;

import androidx.annotation.NonNull;
import androidx.recyclerview.widget.LinearLayoutManager;
import androidx.recyclerview.widget.RecyclerView;
import androidx.viewbinding.ViewBinding;

import com.sk.bilibili.R;
import com.sk.bilibili.callBack.TypeClickCallBack;
import com.sk.bilibili.databinding.BottomDialogBaseHorizotalItemBinding;
import com.sk.bilibili.dto.GenericInfo;
import com.sk.bilibili.model.BaseModel;
import com.sk.bilibili.utils.GenericUtils;
import com.sk.bilibili.view.BaseView;
import com.sk.bilibili.viewHolder.BaseViewHolder;

import lombok.SneakyThrows;

public abstract class BaseHorizontalBottomDialog<M extends BaseModel>
        extends me.shaohui.bottomdialog.BaseBottomDialog implements BaseView {

    @Override
    public int getLayoutRes() {
        initModel();
        return R.layout.bottom_dialog_base_horizontal;
    }

    @SneakyThrows
    private void initModel() {
        GenericInfo<ViewBinding, M> parse = GenericUtils.parse(getClass());
        Class<M> modelClass = parse.getModelClass();
        model = modelClass.newInstance();
        model.setView(this);
    }

    protected TextView title1;
    protected TextView title2;
    protected RecyclerView show1;
    protected RecyclerView show2;
    protected int[] images1;
    protected String[] names1;
    protected int[] images2;
    protected String[] names2;
    protected M model;

    @Override
    public void bindView(View v) {
        initImageAndName();
        title1=v.findViewById(R.id.tv_title1);
        title2=v.findViewById(R.id.tv_title2);
        show1=v.findViewById(R.id.rv_show1);
        show2=v.findViewById(R.id.rv_show2);
        Context context = getContext();
        if(images1!=null){
            show1.setLayoutManager(new LinearLayoutManager(context,RecyclerView.HORIZONTAL,false));
            show1.setAdapter(new Adapter(context,images1,names1,this::onType1Click));
        }
        if(images2!=null){
            show2.setLayoutManager(new LinearLayoutManager(context,RecyclerView.HORIZONTAL,false));
            show2.setAdapter(new Adapter(context,images2,names2,this::onType2Click));
        }
        v.findViewById(R.id.tv_cancel).setOnClickListener(view->dismiss());
        afterSetView();
    }

    protected void toast(String msg) {
        Toast.makeText(getContext(), msg, Toast.LENGTH_SHORT).show();
    }

    protected abstract void afterSetView();

    @Override
    public void loadError() {
        Log.e("loadError",getClass().toString());
    }

    protected abstract void initImageAndName();

    public abstract void onType1Click(int pos);

    public abstract void onType2Click(int pos);

    static class Adapter extends RecyclerView.Adapter<BaseViewHolder<BottomDialogBaseHorizotalItemBinding>>{

        private Context context;
        private int[] images;
        private String[] names;
        private TypeClickCallBack typeClickCallBack;

        public Adapter(Context context, int[] images, String[] names, TypeClickCallBack typeClickCallBack) {
            this.context = context;
            this.images = images;
            this.names = names;
            this.typeClickCallBack = typeClickCallBack;
        }

        @NonNull
        @Override
        public BaseViewHolder<BottomDialogBaseHorizotalItemBinding> onCreateViewHolder(
                @NonNull ViewGroup parent, int viewType) {
            return new BaseViewHolder<>(BottomDialogBaseHorizotalItemBinding.inflate(LayoutInflater.from(context)));
        }

        @Override
        public void onBindViewHolder(@NonNull BaseViewHolder<BottomDialogBaseHorizotalItemBinding> holder,
                                     int position) {
            BottomDialogBaseHorizotalItemBinding bind = holder.getBind();
            bind.ivShow.setImageResource(images[position]);
            bind.tvShow.setText(names[position]);
            bind.getRoot().setOnClickListener(v->typeClickCallBack.onTypeClick(position));
        }

        @Override
        public int getItemCount() {
            return images.length;
        }

    }

}